package cn.iwit.util;

import java.util.ArrayList;
import java.util.List;

public class TreeNodeUtil {
    /**
     * @param treeDataList
     * @return
     */
    public final static List<Tree> getfatherNode(List<Tree> treeDataList) {
        List<Tree> newTreeDataList = new ArrayList<Tree>();
        for (Tree Tree : treeDataList) {
            if (Tree.getPid().equals("0")) {
                //获取父节点下的子节点
                Tree.setChildren(getChildrenNode(Tree.getId(), treeDataList));
                Tree.setExpanded(true);
                newTreeDataList.add(Tree);
            }
        }
        return newTreeDataList;
    }

    /**
     * @param pid
     * @param treeDataList
     * @return
     */
    private final static List<Tree> getChildrenNode(String pid, List<Tree> treeDataList) {
        List<Tree> newTreeDataList = new ArrayList<Tree>();
        for (Tree Tree : treeDataList) {
            if (Tree.getPid() == null) continue;
            //这是一个子节点
            if (Tree.getPid().equals(pid)) {
                //递归获取子节点下的子节点
                Tree.setChildren(getChildrenNode(Tree.getId(), treeDataList));
                newTreeDataList.add(Tree);
            }
        }
        return newTreeDataList;
    }
}
